package ru.dc.object.commands;

import org.apache.commons.lang.StringUtils;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 11.10.2010
 * Time: 17:42:37
 * </pre>
 */
public class SR extends Command {

    private String sourceNick;
    private String filename;
    private long size;
    private boolean directory;
    private int freeSlots;
    private int totalSlots;
    private String hubname;
    private String hubIp;
    private int port;
    private String targetNick;

    @Override
    public String generateCommand() {
        StringBuilder sb = new StringBuilder();
        sb.append(getActualCommandName()).append(" ");
        sb.append(getSourceNick()).append(" ");
        sb.append(getFilename());
        char[] delim = new char[] {(char)5};
        String dm = new String(delim);
        if (!isDirectory()) {
            sb.append(new String(dm));
        }
        sb.append(getSize()).append(" ");

        sb.append(getFreeSlots()).append("/").append(getTotalSlots());
        sb.append(dm);
        sb.append(getHubname());
        sb.append(" (");
        sb.append(getHubIp());
        sb.append(":").append(getPort()).append(")");
        if (!StringUtils.isEmpty(targetNick)) {
            sb.append(dm).append(targetNick);
        }
        

        return sb.toString();
    }

    @Override
    public String getActualCommandName() {
        return "$SR";
    }

    @Override
    public void parseCommand() {
        int nickFirstIndex = 4;
        int nickLastIndex = getCommand().indexOf(" ", nickFirstIndex);
        setSourceNick(getCommand().substring(nickFirstIndex, nickLastIndex));

        int filenameFirstIndex = nickLastIndex+1;
        int filenameLastIndex = getCommand().indexOf((char)5, filenameFirstIndex);
        int dirLastIndex = getCommand().indexOf("/", filenameFirstIndex);
        int sizeLastIndex;
        if (dirLastIndex>filenameLastIndex) {
            setFilename(getCommand().substring(filenameFirstIndex, filenameLastIndex));

            int sizeFirstIndex = filenameLastIndex+1;
            sizeLastIndex = getCommand().indexOf(" ", sizeFirstIndex);
            String sizeStr = getCommand().substring(sizeFirstIndex, sizeLastIndex);
            setSize(new Long(sizeStr));

            setDirectory(false);
        }
        else {
            int pos = dirLastIndex;
            while (getCommand().charAt(pos)!=' ' && pos>filenameFirstIndex) {
                pos--;
            }
            setFilename(getCommand().substring(filenameFirstIndex, pos));
            setDirectory(true);
            setSize(0);
            sizeLastIndex = pos;
        }


        int freeFirstIndex = sizeLastIndex+1;
        int freeLastIndex = getCommand().indexOf("/", freeFirstIndex);
        String freeStr = getCommand().substring(freeFirstIndex, freeLastIndex);
        setFreeSlots(new Integer(freeStr));

        int totalFirstIndex = freeLastIndex+1;
        int totalLastIndex = getCommand().indexOf((char)5, totalFirstIndex);
        String totalStr = getCommand().substring(totalFirstIndex, totalLastIndex);
        setTotalSlots(new Integer(totalStr));

        int hubnameFirstIndex = totalLastIndex+1;
        int hubnameLastIndex = getCommand().indexOf(" ", hubnameFirstIndex);
        setHubname(getCommand().substring(hubnameFirstIndex, hubnameLastIndex));

        int ipFirstIndex = hubnameLastIndex+2;//" ("
        int ipLastIndex = getCommand().indexOf(":", ipFirstIndex);
        int portLastIndex;
        if (ipLastIndex<0) {//если порт не указан
            ipLastIndex = getCommand().indexOf(")", ipFirstIndex);
            portLastIndex = ipLastIndex;
            setHubIp(getCommand().substring(ipFirstIndex, ipLastIndex));
            setPort(411);
        }
        else {
            setHubIp(getCommand().substring(ipFirstIndex, ipLastIndex));

            int portFirstIndex = ipLastIndex+1;
            portLastIndex = getCommand().indexOf(")", portFirstIndex);
            String portStr = getCommand().substring(portFirstIndex, portLastIndex);
            setPort(new Integer(portStr));

        }

        int targetNickFirstIndex = getCommand().indexOf((char) 5, portLastIndex);
        if (targetNickFirstIndex >= 0) {
            int targetNickLastIndex = getCommand().length();
            setTargetNick(getCommand().substring(targetNickFirstIndex, targetNickLastIndex));
        }

        
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public int getFreeSlots() {
        return freeSlots;
    }

    public void setFreeSlots(int freeSlots) {
        this.freeSlots = freeSlots;
    }

    public String getHubIp() {
        return hubIp;
    }

    public void setHubIp(String hubIp) {
        this.hubIp = hubIp;
    }

    public String getHubname() {
        return hubname;
    }

    public void setHubname(String hubname) {
        this.hubname = hubname;
    }

    public boolean isDirectory() {
        return directory;
    }

    public void setDirectory(boolean directory) {
        this.directory = directory;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public String getSourceNick() {
        return sourceNick;
    }

    public void setSourceNick(String sourceNick) {
        this.sourceNick = sourceNick;
    }

    public String getTargetNick() {
        return targetNick;
    }

    public void setTargetNick(String targetNick) {
        this.targetNick = targetNick;
    }

    public int getTotalSlots() {
        return totalSlots;
    }

    public void setTotalSlots(int totalSlots) {
        this.totalSlots = totalSlots;
    }

    @Override
    public String toString() {
        return "SR{" +
                "directory=" + directory +
                ", sourceNick='" + sourceNick + '\'' +
                ", filename='" + filename + '\'' +
                ", size=" + size +
                ", freeSlots=" + freeSlots +
                ", totalSlots=" + totalSlots +
                ", hubname='" + hubname + '\'' +
                ", hubIp='" + hubIp + '\'' +
                ", port=" + port +
                ", targetNick='" + targetNick + '\'' +
                '}';
    }
}
